//
//  PullDownListViewController.m
//  JxCarios
//
//  Created by xcq on 15/3/5.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//


#import "PullDownListViewController.h"
#import "TQMultistageTableView.h"
#import "CompletedOrderView.h"
#import "CarAroundDataSource.h"
#import "TkAndOilCardView.h" //TkAndOilCard //退款和油卡都用这个
#import "TkAndOilCardTableCell.h"
#import "FinishTableViewCell.h"     //finish
static NSString *tkAndOilCard = @"TkAndOilCard";
static NSString *finish = @"finish";
@interface PullDownListViewController () <TQTableViewDataSource,TQTableViewDelegate> {
    CompletedOrderView *view;
    TkAndOilCardView *tkView;
    CurrentDataSource *dataSource;
    NSInteger _orderType;
    NSString *_orderId;
    NSMutableDictionary *recordSelete;
}
@property (nonatomic, strong) CarAroundDataSource *carAround;
@property (nonatomic, strong) TQMultistageTableView *mTableView;
@property (assign , nonatomic) int currenPage;
@property (retain , nonatomic) NSString *totalPage;

@property (nonatomic , strong) NSMutableArray *tableDataSource;

@end

@implementation PullDownListViewController

- (id)init:(PayType)type {
    if(self = [super init]){
        self.type = type;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _currenPage = 1;
    [self initAbout];
    [self netRequest];
}

- (void)netRequest {
    if (!_carAround) {
        _carAround = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    if (_type == refund) { //退款订单
        _carAround.page = INTCONVERSTR(_currenPage);
        [_carAround refund:^(id result) {
            [self setUpTableDataSource:result];
        } anfFail:^(id result) {
            
        }];
        
    }else if (_type == OkValue){   //已完成订单
        _carAround.dic_type = INTCONVERSTR(_type);
        _carAround.page =  INTCONVERSTR(_currenPage);
        [_carAround getOrderDetail:^(id result) {
            [self setUpTableDataSource:result];
        } anfFail:^(id result) {
            
        }];
    }else { //油卡订单
        _carAround.page = INTCONVERSTR(_currenPage);
        [_carAround getOilCardOrder:^(id result) {
            [self setUpTableDataSource:result];
        } anfFail:^(id result) {
            
        }];
        
    }
}

- (void)setUpTableDataSource:(id)result {
    _tableDataSource = [NSMutableArray  arrayWithArray:[result customObjectForkey:@"orderlist"]];
    _totalPage = [NSString stringWithFormat:@"%@",[result customObjectForkey:@"totalpages"]];
    [self initDataSource];
}

- (void)initDataSource {
    if (_tableDataSource.count == 0) {
        return;
    }
    _mTableView.delegate = self;
    _mTableView.dataSource = self;
    [_mTableView setTableviewDelegateAndDataSource];
    [_mTableView reloadData];
}

- (void)footerRefresh {
    if ([_totalPage integerValue] <= _currenPage) {
        [self.view.window makeToast:@"没有更多数据了哦"];
        [_mTableView.tableView footerEndRefreshing];
        return;
    }
    _currenPage ++;
    if (_type == refund) { //退款订单
        _carAround.page = INTCONVERSTR(_currenPage);
        [_carAround refund:^(id result) {
            [_tableDataSource addObjectsFromArray:[NSMutableArray arrayWithArray:[result customObjectForkey:@"orderlist"]]];
            [_mTableView.tableView footerEndRefreshing];
            
            [_mTableView.tableView reloadData];
        } anfFail:^(id result) {
            
            _currenPage --;
            [_mTableView.tableView footerEndRefreshing];
            
        }];
        
    }else if (_type == OkValue){   //已完成订单
        _carAround.dic_type = INTCONVERSTR(_type);
        _carAround.page =  INTCONVERSTR(_currenPage);
        [_carAround getOrderDetail:^(id result) {
            [_tableDataSource addObjectsFromArray:[NSMutableArray arrayWithArray:[result customObjectForkey:@"orderlist"]]];
            [_mTableView.tableView footerEndRefreshing];
            
            [_mTableView.tableView reloadData];
        } anfFail:^(id result) {
            
            _currenPage --;
            [_mTableView.tableView footerEndRefreshing];
  
        }];
    }else { //油卡订单
        _carAround.page = INTCONVERSTR(_currenPage);
        [_carAround getOilCardOrder:^(id result) {
            [_tableDataSource addObjectsFromArray:[NSMutableArray arrayWithArray:[result customObjectForkey:@"orderlist"]]];
            [_mTableView.tableView footerEndRefreshing];
            
            [_mTableView.tableView reloadData];
        } anfFail:^(id result) {
            
            _currenPage --;
            [_mTableView.tableView footerEndRefreshing];
            
        }];
        
    }
    
    
}

- (void)initAbout {
    if (!_mTableView) {
        
        _mTableView = [[TQMultistageTableView alloc] initWithFrame:[RextExtends getScreenFrame]];
        [_mTableView.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
        _mTableView.tableView.tableFooterView = [UIView new];
        [self.view addSubview:_mTableView];
        if (_type == OkValue) {
            view = [[[NSBundle mainBundle] loadNibNamed:@"CompletedOrderView" owner:self options:nil] lastObject];
            view.target = self;
            view.action = @selector(pingjiaAction:);
            [view setFrame:CGRectMake(0, 0, [RextExtends getScreenFrame].size.width, 60)];
             
            //最内层次菜单
            self.mTableView.atomView = view;
        }else {
            tkView = [[[NSBundle mainBundle] loadNibNamed:@"TkAndOilCardView" owner:self options:nil] lastObject];
            
            //最内层次菜单
            self.mTableView.atomView = tkView;
        }
        
    }
    if (_type == refund) {
         //退款订单
        self.title = @"退款订单";
         [_mTableView.tableView registerNib:[UINib nibWithNibName:@"TkAndOilCardTableCell" bundle:nil]  forCellReuseIdentifier:tkAndOilCard];
    }else if (_type == OkValue) {
         //已完成
        self.title = @"已完成订单";
         [_mTableView.tableView registerNib:[UINib nibWithNibName:@"FinishTableViewCell" bundle:nil]  forCellReuseIdentifier:finish];

    }else {
        //油卡订单
        self.title = @"油卡订单";
         [_mTableView.tableView registerNib:[UINib nibWithNibName:@"TkAndOilCardTableCell" bundle:nil]  forCellReuseIdentifier:tkAndOilCard];
    }
    [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:nil andCustom:NO];
    
}


- (void)pingjiaAction:(NSString *) orderId{
     _orderId = orderId;
    if(!dataSource){
        dataSource = [CurrentDataSource dataSourceWithRequestor:self];
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"服务评价"
                                                    message:@"商家已完成订单服务\n可以对该服务进行评价"
                                                   delegate:self
                                          cancelButtonTitle:@"赞一个"
                                          otherButtonTitles:@"还需努力", nil];
    alert.tag = 202;
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *goodOrNo;
    if (buttonIndex == 0) {
        goodOrNo = @"1";
    }else {
        goodOrNo = @"0";
    }
    dataSource.orderId = _orderId;
    dataSource.goodOrno = goodOrNo;
    [dataSource clickAgree:^(id result) {
        NSLog(@"%@",result);
    } andFail:^(id result) {
        NSLog(@"%@",result);
        
    }];
}


#pragma mark - TQTableViewDataSource

#pragma mark - 二级菜单数量
- (NSInteger)mTableView:(TQMultistageTableView *)mTableView numberOfRowsInSection:(NSInteger)section
{
    return _tableDataSource.count == 0 ? 0 : _tableDataSource.count;
}

#pragma mark - 二级菜单内容
- (UITableViewCell *)mTableView:(TQMultistageTableView *)mTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_type == refund) {
        //退款订单
        TkAndOilCardTableCell *cell = [mTableView dequeueReusableCellWithIdentifier:tkAndOilCard forIndexPath:indexPath];
        cell.tag = 300 + indexPath.row;
        cell.payScope.text = [[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"GOOD_NAME"];
        if ([[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"REFUND_STATE"] isEqualToString:@"1"]) {
            cell.payState.text = @"待审核";
        }else if ([[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"REFUND_STATE"] isEqualToString:@"2"]) {
            cell.payState.text = @"审核通过";
        }else if ([[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"REFUND_STATE"] isEqualToString:@"3"]) {
            //拒绝退款
            cell.payState.text = @"拒绝退款";
        }else if ([[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"REFUND_STATE"] isEqualToString:@"4"]) {
            cell.payState.text = @"退款成功";
        }else {
            cell.payState.text = @"退款失败";
        }
        cell.time.text = [[[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"CREATE_DATE"] componentsSeparatedByString:@" "] firstObject];
        cell.orderId.text = [[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"ORDER_ID"];
        if (indexPath.row == [[recordSelete objectForKey:@"cell"] integerValue]) {
            cell.checkBtn.selected = [[recordSelete objectForKey:@"selete"] boolValue];
        }
        return cell;
    }else if (_type == OkValue) {
        //已完成
        FinishTableViewCell *cell = [mTableView dequeueReusableCellWithIdentifier:finish forIndexPath:indexPath];
        cell.shopName.text = [[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"GOOD_NAME"];
        cell.shopPrice.text = [[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"ORDER_PRICE"] stringByAppendingString:@"元"];
        cell.tag = 300 + indexPath.row;
        if (indexPath.row  == [[recordSelete objectForKey:@"cell"] integerValue]) {
            cell.checkBtn.selected = [[recordSelete objectForKey:@"selete"] boolValue];
        }
        return cell;
    }else {
    //油卡订单
        TkAndOilCardTableCell *cell = [mTableView dequeueReusableCellWithIdentifier:tkAndOilCard forIndexPath:indexPath];
        NSString *str = @"";
        
        cell.payScope.text = [[@"油卡充值" stringByAppendingString:[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"VB"]] stringByAppendingString:@"积分"];
        if ([[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"CHONGZHI_STATE"] isEqualToString:@"1"]) {
            cell.payState.text = @"充值成功";
        }else if ([[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"CHONGZHI_STATE"] isEqualToString:@"2"]) {
            cell.payState.text = @"充值失败";
        }else {
            cell.payState.text = @"正在办理充值";
   
        }
        cell.tag = 300 + indexPath.row;
        if (indexPath.row  == [[recordSelete objectForKey:@"cell"] integerValue]) {
            cell.checkBtn.selected = [[recordSelete objectForKey:@"selete"] boolValue];
        }
        cell.time.text = [[[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"CREATE_DATE"] componentsSeparatedByString:@" "] firstObject];
        cell.orderId.text = [[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"ORDER_ID"];
        return cell;
    }
    return nil;
}


#pragma mark - Table view delegate

- (CGFloat)mTableView:(TQMultistageTableView *)mTableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

- (CGFloat)mTableView:(TQMultistageTableView *)mTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_type == OkValue) {
        return 50;
    }else {
        return 60;
    }
}

- (CGFloat)mTableView:(TQMultistageTableView *)mTableView heightForAtomAtIndexPath:(NSIndexPath *)indexPath
{
    if(_type == OkValue){
        return 60;
        ;
    }else {
        return 150;
    }
   
}

- (void)mTableView:(TQMultistageTableView *)mTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}

#pragma mark - Header Open Or Close
//
//- (void)mTableView:(TQMultistageTableView *)mTableView willOpenHeaderAtSection:(NSInteger)section
//{
//    NSLog(@"Open Header ----%ld",section);
//}
//
//- (void)mTableView:(TQMultistageTableView *)mTableView willCloseHeaderAtSection:(NSInteger)section
//{
//    NSLog(@"Close Header ---%ld",section);
//}

#pragma mark - Row Open Or Close

- (void)mTableView:(TQMultistageTableView *)mTableView willOpenRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(!recordSelete){
        recordSelete = [NSMutableDictionary new];
    }
    NSString *tag = nil;
    if (_type == OkValue) { //已完成
         FinishTableViewCell *cell = (FinishTableViewCell *)[mTableView.tableView cellForRowAtIndexPath:indexPath];
        view.name.text = cell.shopName.text;
        NSLog(@"%d",cell.checkBtn.selected);
         view.orderId.text = [@"订单号:" stringByAppendingString:[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"ORDER_ID"]];
        cell.checkBtn.selected = YES;
        NSLog(@"%d",cell.checkBtn.selected);
        tag = [NSString stringWithFormat:@"%ld",cell.tag % 300];
        [recordSelete setObject:tag forKey:@"cell"];
        [recordSelete setObject:@(cell.checkBtn.selected) forKey:@"selete"];
    }else {
        TkAndOilCardTableCell *cell = (TkAndOilCardTableCell *)[mTableView.tableView cellForRowAtIndexPath:indexPath];
        if (_type == OilCard) {
                if ([[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"CHONGZHI_STATE"] isEqualToString:@"1"]) {
                     tkView.stateOne.text = @"充值成功";
                    tkView.stateTwoBtn.highlighted = YES;
                    [tkView.stateBtn setTitle:@"充值成功" forState:UIControlStateNormal];
                    [tkView.stateBtn setBackgroundColor:BTN_COLOR];

                }else if ([[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"CHONGZHI_STATE"] isEqualToString:@"2"]) {
                    [tkView.stateBtn setTitle:@"充值失败" forState:UIControlStateNormal];
                }else {
                   tkView.stateOne.text = @"正在办理充值";
                    
                }
            tkView.detail.text = @"油卡充值";
            tkView.stateTwo.text = @"银行待处理";
            tkView.stateOneTime.text = [[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"CREATE_DATE"];
               tkView.stateTwoTime.text = [[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"CREATE_DATE"];
          
        }else{
            //设置退款id。
            _carAround.oilCard = [[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"ID"];
         
           [_carAround  getTuiKuanDetail:^(id result) {
               tkView.detail.text = [[[result customObjectForkey:@"BANK_OPEN"] stringByAppendingString:@"退款"] stringByAppendingString:[result customObjectForkey:@"REFUND_REALITY_MONEY"]];
               tkView.stateTwo.text = @"银行待处理";
               tkView.stateOneTime.text = [[[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"CREATE_DATE"] componentsSeparatedByString:@" "] firstObject];
               tkView.stateTwoTime.text = [[[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"CREATE_DATE"] componentsSeparatedByString:@" "] firstObject];
               if ([[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"REFUND_STATE"] isEqualToString:@"1"]) {
                   tkView.stateOne.text = @"正在审核";
               }else if ([[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"REFUND_STATE"] isEqualToString:@"2"]) {
                   tkView.stateOne.text = @"审核通过";
               }else if ([[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"REFUND_STATE"] isEqualToString:@"3"]) {
                   //拒绝退款
                   tkView.stateOne.text = @"拒绝退款";
                   [tkView.stateBtn setTitle:@"拒绝退款" forState:UIControlStateNormal];
               }else if ([[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"REFUND_STATE"] isEqualToString:@"4"]) {
                   
                   tkView.stateOne.text = @"充值成功";
                   tkView.stateTwoBtn.highlighted = YES;
                   [tkView.stateBtn setTitle:@"充值成功" forState:UIControlStateNormal];
                   [tkView.stateBtn setBackgroundColor:BTN_COLOR];
               }else {
                   tkView.stateOne.text = @"退款失败";
                   [tkView.stateBtn setTitle:@"退款失败" forState:UIControlStateNormal];               }
           } anfFail:^(id result) {
               
           }];
        }
        cell.checkBtn.selected = YES;
        tag = [NSString stringWithFormat:@"%ld",cell.tag % 300];
        [recordSelete setObject:tag forKey:@"cell"];
        [recordSelete setObject:@(cell.checkBtn.selected) forKey:@"selete"];
    }
}


- (void)mTableView:(TQMultistageTableView *)mTableView willCloseRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSString *tag = nil;
    if (_type == OkValue) { //已完成
        FinishTableViewCell *cell = (FinishTableViewCell *)[mTableView.tableView cellForRowAtIndexPath:indexPath];
        cell.checkBtn.selected = NO;
        tag = [NSString stringWithFormat:@"%ld",cell.tag % 300];
        [recordSelete setObject:@(cell.checkBtn.selected) forKey:@"selete"];
    }else {
        TkAndOilCardTableCell *cell = (TkAndOilCardTableCell *)[mTableView.tableView cellForRowAtIndexPath:indexPath];
        cell.checkBtn.selected = NO;
        tag = [NSString stringWithFormat:@"%ld",cell.tag % 300];
        [recordSelete setObject:@(cell.checkBtn.selected) forKey:@"selete"];
    }
    [recordSelete setObject:tag forKey:@"cell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

 

@end
